<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="任务id" prop="workId">
      <el-input v-model="dataForm.workId" placeholder="任务id"></el-input>
    </el-form-item>
    <el-form-item label="任务名" prop="workName">
      <el-input v-model="dataForm.workName" placeholder="任务名"></el-input>
    </el-form-item>
    <el-form-item label="手机数量" prop="phoneNum">
      <el-input v-model="dataForm.phoneNum" placeholder="手机数量"></el-input>
    </el-form-item>
    <el-form-item label="手机编号集合" prop="phoneIds">
      <el-input v-model="dataForm.phoneIds" placeholder="手机编号集合"></el-input>
    </el-form-item>
    <el-form-item label="直播地址" prop="liveAddr">
      <el-input v-model="dataForm.liveAddr" placeholder="直播地址"></el-input>
    </el-form-item>
    <el-form-item label="评论开始" prop="commentDelayStart">
      <el-input v-model="dataForm.commentDelayStart" placeholder="评论开始"></el-input>
    </el-form-item>
    <el-form-item label="评论结束" prop="commentDelayEnd">
      <el-input v-model="dataForm.commentDelayEnd" placeholder="评论结束"></el-input>
    </el-form-item>
    <el-form-item label="发送开始" prop="commentSendStart">
      <el-input v-model="dataForm.commentSendStart" placeholder="发送开始"></el-input>
    </el-form-item>
    <el-form-item label="发送结束" prop="commentSendEnd">
      <el-input v-model="dataForm.commentSendEnd" placeholder="发送结束"></el-input>
    </el-form-item>
    <el-form-item label="评论类型" prop="commentType">
      <el-input v-model="dataForm.commentType" placeholder="评论类型"></el-input>
    </el-form-item>
    <el-form-item label="时长" prop="time">
      <el-input v-model="dataForm.time" placeholder="时长"></el-input>
    </el-form-item>
    <el-form-item label="是否@" prop="isAt">
      <el-input v-model="dataForm.isAt" placeholder="是否@"></el-input>
    </el-form-item>
    <el-form-item label="间隔" prop="sendGap">
      <el-input v-model="dataForm.sendGap" placeholder="间隔"></el-input>
    </el-form-item>
    <el-form-item label="关键词" prop="keyWords">
      <el-input v-model="dataForm.keyWords" placeholder="关键词"></el-input>
    </el-form-item>
    <el-form-item label="已回复数量" prop="respNum">
      <el-input v-model="dataForm.respNum" placeholder="已回复数量"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          workId: '',
          workName: '',
          phoneNum: '',
          phoneIds: '',
          liveAddr: '',
          commentDelayStart: '',
          commentDelayEnd: '',
          commentSendStart: '',
          commentSendEnd: '',
          commentType: '',
          time: '',
          isAt: '',
          sendGap: '',
          keyWords: '',
          respNum: '',
          createTime: ''
        },
        dataRule: {
          workId: [
            { required: true, message: '任务id不能为空', trigger: 'blur' }
          ],
          workName: [
            { required: true, message: '任务名不能为空', trigger: 'blur' }
          ],
          phoneNum: [
            { required: true, message: '手机数量不能为空', trigger: 'blur' }
          ],
          phoneIds: [
            { required: true, message: '手机编号集合不能为空', trigger: 'blur' }
          ],
          liveAddr: [
            { required: true, message: '直播地址不能为空', trigger: 'blur' }
          ],
          commentDelayStart: [
            { required: true, message: '评论开始不能为空', trigger: 'blur' }
          ],
          commentDelayEnd: [
            { required: true, message: '评论结束不能为空', trigger: 'blur' }
          ],
          commentSendStart: [
            { required: true, message: '发送开始不能为空', trigger: 'blur' }
          ],
          commentSendEnd: [
            { required: true, message: '发送结束不能为空', trigger: 'blur' }
          ],
          commentType: [
            { required: true, message: '评论类型不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '时长不能为空', trigger: 'blur' }
          ],
          isAt: [
            { required: true, message: '是否@不能为空', trigger: 'blur' }
          ],
          sendGap: [
            { required: true, message: '间隔不能为空', trigger: 'blur' }
          ],
          keyWords: [
            { required: true, message: '关键词不能为空', trigger: 'blur' }
          ],
          respNum: [
            { required: true, message: '已回复数量不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/admin/managerwork/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.workId = data.managerWork.workId
                this.dataForm.workName = data.managerWork.workName
                this.dataForm.phoneNum = data.managerWork.phoneNum
                this.dataForm.phoneIds = data.managerWork.phoneIds
                this.dataForm.liveAddr = data.managerWork.liveAddr
                this.dataForm.commentDelayStart = data.managerWork.commentDelayStart
                this.dataForm.commentDelayEnd = data.managerWork.commentDelayEnd
                this.dataForm.commentSendStart = data.managerWork.commentSendStart
                this.dataForm.commentSendEnd = data.managerWork.commentSendEnd
                this.dataForm.commentType = data.managerWork.commentType
                this.dataForm.time = data.managerWork.time
                this.dataForm.isAt = data.managerWork.isAt
                this.dataForm.sendGap = data.managerWork.sendGap
                this.dataForm.keyWords = data.managerWork.keyWords
                this.dataForm.respNum = data.managerWork.respNum
                this.dataForm.createTime = data.managerWork.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/managerwork/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'workId': this.dataForm.workId,
                'workName': this.dataForm.workName,
                'phoneNum': this.dataForm.phoneNum,
                'phoneIds': this.dataForm.phoneIds,
                'liveAddr': this.dataForm.liveAddr,
                'commentDelayStart': this.dataForm.commentDelayStart,
                'commentDelayEnd': this.dataForm.commentDelayEnd,
                'commentSendStart': this.dataForm.commentSendStart,
                'commentSendEnd': this.dataForm.commentSendEnd,
                'commentType': this.dataForm.commentType,
                'time': this.dataForm.time,
                'isAt': this.dataForm.isAt,
                'sendGap': this.dataForm.sendGap,
                'keyWords': this.dataForm.keyWords,
                'respNum': this.dataForm.respNum,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
